#include "Game Engine/Header/OBB.h"
#include "Game Engine/Header/Physics.h"
#include "Game Engine/Header/Camera.h"

OBB::OBB( D3DXVECTOR3 pos, D3DXVECTOR3 size, bool dynamic )
{
	//Create Mesh
	mMeshData = NULL;

	//Create Motion State
	btQuaternion q(0.0f, 0.0f, 0.0f);
	btVector3 p(pos.x, pos.y, pos.z);
	btTransform startTrans(q, p);
	btMotionState *ms = new btDefaultMotionState(startTrans);

	//Create Collision Shape
	btCollisionShape *cs = new btBoxShape(btVector3(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f));
	
	//Create Rigid Body
	float mass = size.x * size.y * size.z;
	btVector3 localInertia(0,0,0);
	cs->calculateLocalInertia(mass,localInertia);
	if(!dynamic)mass = 0.0f;

	mBody = new btRigidBody(mass, ms, cs, localInertia);
}

OBB::~OBB()
{
	Release();
}

void OBB::Release()
{
	if(mMeshData != NULL)
		mMeshData->Release();

	if(mBody != NULL)
		delete mBody;
}

void OBB::Render( Mesh& mesh, D3DXMATRIX& lightViewProj )
{
	btMotionState *ms2 = mBody->getMotionState();
	if(ms2 == NULL)return;

	mesh.Draw( BT2DX_MATRIX(*ms2)/* * world*/, lightViewProj );
}